﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Utilities.QuickSql;
using MySql.Data;

namespace Deviseur_Cs2i_Car
{
    public partial class AddCategOption : Form
    {
        private CATEGOPTION CategOption;

        public AddCategOption()
        {
            InitializeComponent();
            this.Text = "Ajouter un type de voiture";
            this.ButtonSubmitCategOption.Text = "Ajouter";
        }
        public AddCategOption(int NumCateg)
        {
            InitializeComponent();
            this.Text = "Modifier un type de voiture";
            this.ButtonSubmitCategOption.Text = "Modifier";

            Table TypeVoiture = Program.Db.QueryTable("SELECT LibelleCateg FROM CATEGOPTION WHERE NumCateg = " + NumCateg);
            foreach (Record r in TypeVoiture)
            {
                this.LibelleCategOption.Text = r.Values[0].ToString();
                CategOption = new CATEGOPTION(NumCateg, this.LibelleCategOption.Text.ToString());
            }
        }

        private void ButtonSubmitCategOption_Click(object sender, EventArgs e)
        {

            if (this.LibelleCategOption.Text.ToString() != "")
            {
                if (this.ButtonSubmitCategOption.Text == "Ajouter")
                {
                    CategOption = new CATEGOPTION(this.LibelleCategOption.Text.ToString());

                    Thread addThread = new Thread(new ThreadStart(addCategOption_Thread));
                    addThread.Start();
                }
                else if (this.ButtonSubmitCategOption.Text == "Modifier")
                {
                    CategOption.set_LibelleCategOption(this.LibelleCategOption.Text.ToString());

                    Thread updateThread = new Thread(new ThreadStart(updateCategOption_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        }
        private void addCategOption_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            CategOption.new_CategOption();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void updateCategOption_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            CategOption.modify_CategOption();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void Disabled()
        {
            this.LibelleCategOption.Enabled = false;
            this.ButtonSubmitCategOption.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }

    }
}
